<?php

namespace App\Admin\Forms;

use App\Jobs\SubscribeMessageJob;
use App\Jobs\User\UserWithdrawalProcessJob;
use App\Models\User\WxUserWithdrawal;
use App\Wen\Utils\Settings;
use App\Wen\Utils\UserUtils;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Support\Facades\Cache;

class WithdrawalProcessForms extends Form implements LazyRenderable
{
    use LazyWidget;


    /**
     * 主题颜色.
     *
     * @var array
     */
    protected $options = [0=>'受理中', 1=>'已到账', 2=>'异常', 3=>'微信打款中', 4=>'支付宝打款中'];

    /**
     * 处理表单请求.
     *
     * @param array $input
     *
     * @return mixed
     */
    public function handle(array $input)
    {

        $id = $input['id'];
        $push_state = $input['state'];
        $refuse_tip = _array_key($input, 'refuse_tip', '');
        $accept_tip = _array_key($input, 'accept_tip', '');

        if(_empty_($push_state, true)){
            return $this->response()
                ->warning('操作失败.')
                ->refresh();
        }

        $wxUserWithdrawal = WxUserWithdrawal::find($id);
        if($wxUserWithdrawal->is_lock == 1){
            return $this->response()
                ->warning('稍后再试.')
                ->refresh();
        }
        if(_empty_($wxUserWithdrawal)){
            return $this->response()
                ->warning('操作失败.')
                ->refresh();
        }
        $current_state = $wxUserWithdrawal->state;

        if($current_state == 0){
            if($push_state == 0){

            }else if($push_state == 1){
                // 提现处理
                $r = WxUserWithdrawal::where('id', $id)->update([
                    'state' => 1, 'refuse_tip' => ''
                ]);
                if($r){
                    UserUtils::add_user_notice(7001, $wxUserWithdrawal->user_id, '提现到账通知', $accept_tip, 100);
                    SubscribeMessageJob::dispatch('withdrawal', $id);
                }

                Cache::forget('total:todo:count');
                Cache::forget('total:tenant:todo:count');

                return $this->response()
                    ->success('操作成功.')
                    ->refresh();
            }else if($push_state == 2){
                // 提现异常
                $r = WxUserWithdrawal::where('id', $id)->update([
                   'state' => 2, 'refuse_tip' => $refuse_tip
                ]);
                if($r){
                    UserUtils::add_user_notice(7002, $wxUserWithdrawal->user_id, '提现异常通知', $refuse_tip, 100);
                }

                Cache::forget('total:todo:count');
                Cache::forget('total:tenant:todo:count');

                return $this->response()
                    ->success('操作成功.')
                    ->refresh();
            }else if($push_state == 3){
                UserWithdrawalProcessJob::dispatch($id);
                return $this->response()
                    ->success('已提交给队列处理.')
                    ->refresh();
            }else if($push_state == 4){
                return $this->response()
                    ->success('暂未实现.')
                    ->refresh();
            }
        }else if($current_state == 1){
            return $this->response()
                ->warning('操作失败.该提现已经被处理过了')
                ->refresh();
        }else if($current_state == 2){
            if($push_state == 0){
                return $this->response()
                    ->warning('该提现处于异常状态，若打款完成，请选择“已打款”')
                    ->refresh();
            }else if($push_state == 1){
                // 提现处理
                $r = WxUserWithdrawal::where('id', $id)->update([
                    'state' => 1, 'refuse_tip' => ''
                ]);
                if($r) {
                    UserUtils::add_user_notice(7001, $wxUserWithdrawal->user_id, '提现到账通知', $accept_tip, 100);
                }

                Cache::forget('total:todo:count');
                Cache::forget('total:tenant:todo:count');

                return $this->response()
                    ->success('操作成功.')
                    ->refresh();
            }else if($push_state == 2){
                return $this->response()
                    ->warning('该提现处于异常状态，若打款完成，请选择“已打款”')
                    ->refresh();
            }else if($push_state == 3){
                if(!_empty_($wxUserWithdrawal->wx_batch_id) && !_empty_($wxUserWithdrawal->wechat_phone)){
                    _logger_(__file__, __line__, $wxUserWithdrawal->withdrawal_id.'删除wx_batch_id:' . $wxUserWithdrawal->wx_batch_id);
                    WxUserWithdrawal::where('id', $wxUserWithdrawal->id)->update(['wx_batch_id'=>'', 'state' => 5]);
                }
                UserWithdrawalProcessJob::dispatch($id);
                return $this->response()
                    ->success('已提交给队列处理.')
                    ->refresh();
            }else if($push_state == 4){
                return $this->response()
                    ->success('暂未实现.')
                    ->refresh();
            }
        }else if($current_state == 3){
            return $this->response()
                ->warning('请等待微信打款完成')
                ->refresh();
        }else if($current_state == 4){
            return $this->response()
                ->warning('请等待支付宝打款完成')
                ->refresh();
        }
        return $this->response()
            ->success('暂未实现.')
            ->refresh();
    }

    /**
     * 构建表单.
     */
    public function form()
    {
        $this->hidden('id')->value($this->payload["id"]);
        $withdrawal = WxUserWithdrawal::where('id', $this->payload["id"])->first();
        if($withdrawal){
            $options = [0=>'受理中', 1=>'已到账', 2=>'异常', 3=>'微信打款中', 4=>'支付宝打款中'];
            if($withdrawal->state == 5){
                unset($options[3]);
                $options[5] = '尝试微信重新打款中';
            }
            if($withdrawal->state === 0){
                $options['3'] = '尝试微信打款';
                $options['4'] = '尝试支付宝打款';
            }else if($withdrawal->state ==2 && !_empty_($withdrawal->wx_batch_id) && !_empty_($withdrawal->wechat_phone)){
                $options['3'] = '微信打款失败，尝试重新微信打款';
            }
            $this->radio('state', "状态")->when(1, function (Form $form) use (&$withdrawal){
                $form->textarea('accept_tip', "通知提示")->default('您好，您发起的提现已经到账，请注意查收')->disable(!in_array($withdrawal->state, [0, 2]));
            })->when(2, function (Form $form) use (&$withdrawal){
                $form->textarea('refuse_tip', "异常原因")->default('尊敬的客户您好，根据您预留的支行名称及银行卡号无法转账，请您联系我们客服处理。QQ: '.Settings::get('about_qq'))->disable(!in_array($withdrawal->state, [0, 2]));;
            })->options($options)->default($withdrawal->state)->disable(!in_array($withdrawal->state, [0, 2]));
        }

    }

}
